package com.song.eduservice.controller;


import com.song.commonutils.ResultObj;
import com.song.eduservice.client.ossfeign.OosFeign;
import com.song.eduservice.entity.EduTeacher;
import com.song.eduservice.entity.vo.EduTeacherVo;
import com.song.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-03-27
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @Autowired
    private OosFeign oosFeign;

    @ApiOperation(value = "查询所有讲师")
    @GetMapping("/")
    public ResultObj findAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return ResultObj.ok().data("data",list);
    }
    /**
     * 分页查询与模糊查询讲师
     * @param current
     * @param limit
     * @param eduTeacherVo
     * @return
     */
    @ApiOperation(value = "分页查询与模糊查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Object list(@PathVariable long current,@PathVariable long limit,
                       @RequestBody(required = false) EduTeacherVo eduTeacherVo) {
        return eduTeacherService.pageTeacherCondition(eduTeacherVo,current,limit);
    }


    /**
     * 添加讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("/")
    public ResultObj addEduTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = eduTeacherService.save(eduTeacher);
        if (b) {
            return ResultObj.ADD_SUCCESS;
        } else {
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 逻辑删除讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public ResultObj deleteEduTeacher(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String  id) {
        boolean b = eduTeacherService.removeById(id);
        if (b) {
            return ResultObj.DELETE_SUCCESS;
        } else {
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 更新讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "更新讲师")
    @PutMapping("/")
    public ResultObj updateEduTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b) {
            return ResultObj.UPDATE_SUCCESS;
        } else {
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     *按照ID查询讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "按照ID查询讲师")
    @GetMapping("/findByIdEduTeacher/{id}")
    public ResultObj findByIdEduTeacher(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String id) {
        EduTeacher b = eduTeacherService.getById(id);
        if (b!=null) {
            return ResultObj.ok().data("data",b);
        } else {
            return ResultObj.error();
        }
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @ApiOperation(value = "上传图片")
    @PostMapping("/uploadPic")
    public String  uploadFileAvatar(MultipartFile file){
        //返回上传到oss的路径
        String url=oosFeign.uploadFileAvatar(file);
        return url;
    }

}

